'''

#!Pyhon3
#coding=utf-8
#Target: to change the name/size of picture automatically.
#Author:Terry

'''
import os
from io import BytesIO
from PIL import Image



def rename_all(folder_path, mode=0, prename=None):
    ''' 批量更新文件名'''
    begin_num = 1
    filelist = os.listdir(folder_path)
    if mode == 0:
        for item in filelist:
            # name = item.split(".")[0]
            ex_name = item.split(".")[1]
            path = folder_path+"\\"+item
            new_path = folder_path+"\\"+str(begin_num)+"."+ex_name
            # print(begin_num,name,ex_name)
            os.rename(path, new_path)
            begin_num = begin_num+1
        print(30*'-', "文件名批量更新完成!", 30*'-')
    elif mode == 1:
        for item in filelist:
            # name = item.split(".")[0]
            ex_name = item.split(".")[1]
            path = folder_path+"\\"+item
            new_path = folder_path+"\\"+prename+str(begin_num)+"."+ex_name
            # print(begin_num,name,ex_name)
            os.rename(path, new_path)
            begin_num = begin_num+1
        print(30*'-', "文件名批量更新完成!", 30*'-')
    else:
        print(30*'-', "用法不对", 30*'-')

def resize_all(fname, weight=None, height=None):
    ''' change image size bulkly '''
    pass

def resize_to(file_name):
    ''' adjust the single file size '''
    img = Image.open(file_name)
    # print(img.format, "\n", img.size, "\n", img.mode)
    # 将三通道到jpg转换成四通道的png模式
    new_img=img.convert(mode='RGBA')
    new_img.resize(480,720)
    new_img.save("after.png")
    # img.show()
    print(img.size)

def meger_pic(img_source,img_logo):
    '''meger a logo to a picture
    # open file-a
    # find the position from file-a
    # find the size of file-b
    # paste file-b to file-a at the specified position
    # save the megered file as file-c
    '''
    with open(img_source,'rb') as f:
        img_bg=Image.open(BytesIO(f.read()))
        print(type(img_bg))
        print("文件大小：",img_bg.size)
        print("图片模式：",img_bg.mode)
        # img_bg.save("new1",format='png')
        img_end=Image.new(img_bg.mode,img_bg.size)
    return img_end

# Main Body
# FF = r"C:\Users\Administrator\Downloads\photos"
#rename_all(fs,1,"new")
#rename_all(fs,1,"new")
# rename_all(fs,4,"new")

SF =r"C:\Users\Administrator\Downloads\photos\new1.jpg"

# try:
#     resize_to(SF)
# except Exception as e:
#     raise e
# finally:
#     exit()
